#include "Joystick.hpp"

using namespace XO;


#ifdef ENABLE_DEVBUILD
static int32_t	sJoyIndex								=	0;
static bool		sJoyConnected							=	false;
static uint32_t	sJoyButtonCount							=	0;
static float	sJoyAxis[ sf::Joystick::AxisCount ]		=	{0.0f};
static bool		sJoyButton[ sf::Joystick::ButtonCount ]	=	{0};


TweakUtils::Panel Joystick::DebugPanel()
{
	TweakUtils::Panel::EnumType_t	joyIndex	=	TweakUtils::Panel::EnumType( "Joysticks" )
		.Entry( "1", 0 )
		.Entry( "2", 1 )
		.Entry( "3", 2 )
		.Entry( "4", 3 )
		.Entry( "5", 4 )
		.Entry( "6", 5 )
		.Entry( "7", 6 )
		.Entry( "8", 7 )
		;

	return TweakUtils::Panel::Create( "Joystick Debugger" )
		.Callback( []{ Update(); } )
		.Enumeration( "Joystick", joyIndex, &sJoyIndex ).End()
		.Variable< bool >( "Connected", &sJoyConnected ).End()
		.Variable< uint32_t >( "Button Count", &sJoyButtonCount ).End()
		// All the axis' supported.
		.Separator( "Axis information" ).End()
		.Variable< float >( "Axis X", &sJoyAxis[ 0 ] ).End()
		.Variable< float >( "Axis Y", &sJoyAxis[ 1 ] ).End()
		.Variable< float >( "Axis Z", &sJoyAxis[ 2 ] ).End()
		.Variable< float >( "Axis R", &sJoyAxis[ 3 ] ).End()
		.Variable< float >( "Axis U", &sJoyAxis[ 4 ] ).End()
		.Variable< float >( "Axis V", &sJoyAxis[ 5 ] ).End()
		.Variable< float >( "Axis Pov X", &sJoyAxis[ 6 ] ).End()
		.Variable< float >( "Axis Pov Y", &sJoyAxis[ 7 ] ).End()
		// 16 should be enough.
		.Separator( "Button information" ).End()
		.Variable< bool >( "Button 0", &sJoyButton[ 0 ] ).End()
		.Variable< bool >( "Button 1", &sJoyButton[ 1 ] ).End()
		.Variable< bool >( "Button 2", &sJoyButton[ 2 ] ).End()
		.Variable< bool >( "Button 3", &sJoyButton[ 3 ] ).End()
		.Variable< bool >( "Button 4", &sJoyButton[ 4 ] ).End()
		.Variable< bool >( "Button 5", &sJoyButton[ 5 ] ).End()
		.Variable< bool >( "Button 6", &sJoyButton[ 6 ] ).End()
		.Variable< bool >( "Button 7", &sJoyButton[ 7 ] ).End()
		.Variable< bool >( "Button 8", &sJoyButton[ 8 ] ).End()
		.Variable< bool >( "Button 9", &sJoyButton[ 9 ] ).End()
		.Variable< bool >( "Button 10", &sJoyButton[ 10 ] ).End()
		.Variable< bool >( "Button 11", &sJoyButton[ 11 ] ).End()
		.Variable< bool >( "Button 12", &sJoyButton[ 12 ] ).End()
		.Variable< bool >( "Button 13", &sJoyButton[ 13 ] ).End()
		.Variable< bool >( "Button 14", &sJoyButton[ 14 ] ).End()
		.Variable< bool >( "Button 15", &sJoyButton[ 15 ] ).End()
		;
}


void Joystick::Update()
{
	sJoyConnected	=	sf::Joystick::isConnected( sJoyIndex );
	sJoyButtonCount	=	sJoyConnected ? sf::Joystick::getButtonCount( 0 ) : 0;
	for( int i=0; i<sf::Joystick::AxisCount; ++i )
	{
		if( sJoyConnected && sf::Joystick::hasAxis( sJoyIndex, (sf::Joystick::Axis)i ) )
			sJoyAxis[ i ]	=	sf::Joystick::getAxisPosition( sJoyIndex, (sf::Joystick::Axis)i );
		else
			sJoyAxis[ i ]	=	0.0f;
	}
	for( int i=0; i<sf::Joystick::ButtonCount; ++i )
	{
		if( sJoyConnected )
			sJoyButton[ i ]	=	sf::Joystick::isButtonPressed( sJoyIndex, i );
		else
			sJoyButton[ i ]	=	false;
	}
}
#endif
